package com.tianhui.consignortwofei.mvp.ui.activity.audit.vehicle;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.DictionaryType;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.VehicleLicenseInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.VehicleFrontStrategy;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class VehicleFrontAuditActivity extends BaseVehicleAuditActivity {

    @BindView(R.id.activity_vehicle_front_audit_plateNumberInputItemView)
    InputItemView mPlateNumberInputItemView;
    @BindView(R.id.activity_vehicle_front_audit_ownerInputItemView)
    InputItemView mOwnerInputItemView;
    @BindView(R.id.activity_vehicle_front_audit_natureInputItemView)
    InputItemView mNatureInputItemView;
    @BindView(R.id.activity_vehicle_front_audit_modelInputItemView)
    InputItemView mModelInputItemView;
    @BindView(R.id.activity_vehicle_front_audit_codeInputItemView)
    InputItemView mCodeInputItemView;

    @BindView(R.id.activity_vehicle_front_audit_engineInputItemView)
    InputItemView mEngineInputItemView;
    @BindView(R.id.activity_vehicle_front_audit_officeInputItemView)
    InputItemView mOfficeInputItemView;
    @BindView(R.id.activity_vehicle_front_audit_vehicleTypeClickItemView)
    ClickItemView mVehicleTypeClickItemView;
    @BindView(R.id.activity_vehicle_front_audit_registerDateClickItemView)
    ClickItemView mRegisterDateClickItemView;
    @BindView(R.id.activity_vehicle_front_audit_issueDateClickItemView)
    ClickItemView mIssueDateClickItemView;

    private TimePickerView mRegisterDatePickerView;
    private TimePickerView mIssueDatePickerView;
    private SelectDialog mVehicleDialog;
    private String mFrontImageId;
    private String mVehicleType;

    @Override
    public int getLayoutId() {
        return R.layout.activity_vehicle_front_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }


    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {

        mFrontImageId = pictureInfo.id;
    }

    @Override
    public void showVehicleInfo(VehicleInfo info) {

        mPlateNumberInputItemView.setContent(info.vehiclenum);
        mOwnerInputItemView.setContent(info.owner);
        mNatureInputItemView.setContent(info.usenature);
        mModelInputItemView.setContent(info.brandmodel);
        mCodeInputItemView.setContent(info.vinno);
        mEngineInputItemView.setContent(info.engineno);
        mOfficeInputItemView.setContent(info.issuingorganizations);
        mVehicleTypeClickItemView.setContent(info.vehicletypename);
        mRegisterDateClickItemView.setContent(info.registerdate);
        mIssueDateClickItemView.setContent(info.passportdate);
        loadPicture(info.imgdrivingimage);
        mFrontImageId = info.drivingimage;
        mVehicleType = info.vehicletype;

    }

    @Override
    public VehicleInfo getSaveInfo() {
        String plateNumber = mPlateNumberInputItemView.getContent();
        String owner = mOwnerInputItemView.getContent();
        String nature = mNatureInputItemView.getContent();
        String model = mModelInputItemView.getContent();
        String code = mCodeInputItemView.getContent();
        String engine = mEngineInputItemView.getContent();
        String office = mOfficeInputItemView.getContent();
        String vehicleTypeName = mVehicleTypeClickItemView.getContent();

        String vehicleType = mVehicleType;
        String registerDate = mRegisterDateClickItemView.getContent();
        String issueDate = mIssueDateClickItemView.getContent();
        if (TextUtils.isEmpty(plateNumber)) {
            showToast("请输入车牌号");
            return null;
        }
        if (TextUtils.isEmpty(owner)) {
            showToast("请输入所属人");
            return null;
        }
        if (TextUtils.isEmpty(nature)) {
            showToast("请输入使用性质");
            return null;
        }
        if (TextUtils.isEmpty(model)) {
            showToast("请输入品牌型号");
            return null;
        }
        if (TextUtils.isEmpty(code)) {
            showToast("请输入车辆识别代码");
            return null;
        }
        if (TextUtils.isEmpty(engine)) {
            showToast("请输入发动机号");
            return null;
        }
        if (TextUtils.isEmpty(office)) {
            showToast("请输入发证机关");
            return null;
        }
        if (TextUtils.isEmpty(vehicleTypeName)) {
            showToast("请选择车辆类型");
            return null;
        }
        if (TextUtils.isEmpty(registerDate)) {
            showToast("请选择注册日期");
            return null;
        }
        if (TextUtils.isEmpty(issueDate)) {
            showToast("请选择发证日期");
            return null;
        }

        mVehicleInfo.vehiclenum = plateNumber;
        mVehicleInfo.owner = owner;
        mVehicleInfo.usenature = nature;
        mVehicleInfo.brandmodel = model;
        mVehicleInfo.vinno = code;
        mVehicleInfo.engineno = engine;
        mVehicleInfo.issuingorganizations = office;

        mVehicleInfo.registerdate = registerDate;
        mVehicleInfo.passportdate = issueDate;

        mVehicleInfo.drivingimage = mFrontImageId;
        mVehicleInfo.vehicletype = mVehicleType;
        mVehicleInfo.vehicletypename = vehicleTypeName;
        return mVehicleInfo;
    }

    @Override
    public void nextAudit() {
        VehicleInfo info = getSaveInfo();
        if (info != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_VEHICLE_INFO, info);
            bundle.putInt(KeyValue.KEY_OPERATE, getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
            openActivityForResult(VehicleBackAuditActivity.class, bundle);
        }

    }

    @OnClick(R.id.activity_vehicle_front_audit_changePlateNumberTextView)
    public void changePlateNumber() {
        new MaterialDialog.Builder(this)
                .content("是否确认更换车牌号？")
                .positiveText("是")
                .negativeText("否")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mPlateNumberInputItemView.setItemEnable(true);
                    }
                })
                .show();
    }

    //注册日期
    @OnClick(R.id.activity_vehicle_front_audit_registerDateClickItemView)
    public void selectRegisterDate() {
        hideKeyboard();
        if (mRegisterDatePickerView == null) {
            mRegisterDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mRegisterDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mRegisterDatePickerView.show();
    }

    //发证日期
    @OnClick(R.id.activity_vehicle_front_audit_issueDateClickItemView)
    public void selectIssueDate() {
        hideKeyboard();
        if (mIssueDatePickerView == null) {
            mIssueDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mIssueDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mIssueDatePickerView.show();
    }

    //车辆类型
    @OnClick(R.id.activity_vehicle_front_audit_vehicleTypeClickItemView)
    public void selectVehicleType() {
        hideKeyboard();
        if (null == mVehicleDialog) {
            Map<String, String> map = new HashMap<>();
            map.put("pid", "81");
            requestTypeList(DictionaryType.TYPE_VEHICLE, map, true);
        } else {
            mVehicleDialog.show();
        }
    }

    @Override
    public void showTypeList(int type, List<DictionaryInfo> list) {
        switch (type) {
            case DictionaryType.TYPE_VEHICLE:
                mVehicleDialog = new SelectDialog(this);
                showSelectDialog(DictionaryType.TYPE_VEHICLE, "请选择车辆类型", mVehicleDialog, mVehicleTypeClickItemView, list);
                break;
        }

    }

    @Override
    public void setTypeInfo(int type, com.fgs.common.interf.ISelectItemValue iSelectItemValue) {
        switch (type) {
            case DictionaryType.TYPE_VEHICLE:

                mVehicleType = iSelectItemValue.getSelectItemTextValue();
                break;
        }
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        VehicleFrontStrategy vehicleFrontStrategy = (VehicleFrontStrategy) pictureStrategyFactory.createStrategy(VehicleFrontStrategy.class);
        pictureContext = new PictureContext(vehicleFrontStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<VehicleLicenseInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<VehicleLicenseInfo> resultInfo) {
                mFrontImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
                processVehicleFrontResult(resultInfo.resultInfo);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

    private void processVehicleFrontResult(VehicleLicenseInfo resultInfo) {
        if (resultInfo == null) return;
        mOwnerInputItemView.setContent(resultInfo.owner);
        mNatureInputItemView.setContent(resultInfo.useCharacter);
        mModelInputItemView.setContent(resultInfo.model);
        mCodeInputItemView.setContent(resultInfo.vin);
        mEngineInputItemView.setContent(resultInfo.engineNo);
        mOfficeInputItemView.setContent(resultInfo.seal);
        mRegisterDateClickItemView.setContent(resultInfo.registerDate);
        mIssueDateClickItemView.setContent(resultInfo.issueDate);
    }

}
